package com.nursinghome.admin.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.nursinghome.admin.service.MedicalStaffService;
import com.nursinghome.admin.service.UserService;
import com.nursinghome.bean.RespBean;
import com.nursinghome.bean.SysLog;
import com.nursinghome.bean.User;
import com.nursinghome.common.service.SysLogService;
import com.nursinghome.exception.NurisingHomeException;
import com.nursinghome.utils.JwtUtils;
import com.nursinghome.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping
@Validated
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MedicalStaffService medicalStaffService;
    @Autowired
    private SysLogService  userLoginService;
    @Autowired
    private RedisUtil redisUtil;
    //登录认证
    @PostMapping("/login")
    public RespBean login(@NotBlank(message = "用户名不能为空") String username,
                          @NotBlank(message = "密码不能为空") String password,
                          @NotBlank(message = "验证码不能为空") String capath,
                          String capathID
                          ) throws NurisingHomeException {
        User user = userService.login(username, password);

        //判断验证码是否为空
        String capathIDRedis = (String)redisUtil.get(capathID);
        if ( capathIDRedis == null || (capathIDRedis != null && !capathIDRedis.equalsIgnoreCase(capath))){
            return RespBean.error("验证码错误或失效,请重新刷刷新");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", user.getId());
        map.put("username", user.getUsername());
        map.put("character",user.getCharacter());
        if (user.getCharacter() == 2){
            //根据员工用户名查询对应的mid
            Integer mid = medicalStaffService.selectByMusername(user.getUsername());
            map.put("mid",mid);
        }

        String jwt = JwtUtils.generateJwt(map);

        return RespBean.ok("登录成功",jwt);
    }
    //后台验证码的生成
    @GetMapping("/capath")
    public RespBean getCapath(){
        LineCaptcha captcha = new LineCaptcha(120, 38, 4, 20);
        String code = captcha.getCode();
        String capathID = IdUtil.getSnowflakeNextIdStr();
        //将验证码放入redis中
        redisUtil.set(capathID, code,60);

        String imageBase64Data = captcha.getImageBase64Data();
        HashMap<String, Object> map = new HashMap<>();
        map.put("imageBase64Data",imageBase64Data);
        map.put("capathID",capathID);

        return RespBean.ok("",map);
    }
    //获取用户的信息
    @GetMapping("/admin/userInfo")
    public RespBean userInfo(@RequestHeader(value = "token", required = false) String token){
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer id = (Integer) map.get("id");
        User user = userService.selectById(id);
        return RespBean.ok("",user);
    }

    //修改密码
    @PutMapping("/updatePassword")
    public RespBean updatePassword(String username,
                                   String password,
                                   String newPassword,
                                   String confirmPassword) throws NurisingHomeException {
        if (userService.updateByUse(username, password, newPassword, confirmPassword)) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
    //日志信息查询
    @GetMapping("/admin/log")
    public RespBean selectByUserLog (@RequestHeader("token") String token){

        List<SysLog> list = userLoginService.selectByUserName(null);

        return RespBean.ok("",list);
    }
}
